package com.twu.refactoring;

public class OrderReceipt {
    private Order order;

    public OrderReceipt(Order order) {
        this.order = order;
	}

	public String printReceipt() {
		StringBuilder output = new StringBuilder();
        output.append(appendCostumerInfo());
        output.append(appendLineItems());
        output.append(appendTotal());
		return output.toString();
	}

    private String appendTotal() {
        String print = "";
        double totSalesTx = 0d;
        double tot = 0d;
        double salesTax = 0d;

        for (LineItem lineItem : order.getLineItems()) {
            salesTax = lineItem.totalAmount() * 0.10;
            totSalesTx += salesTax;
            tot += lineItem.totalAmount() + salesTax;
        }
        print += "Sales Tax\t" + totSalesTx;
        print += "Total Amount\t" + tot;
        return print;
    }

    private String appendLineItems() {
        String print = "";
        for (LineItem lineItem : order.getLineItems()) {
            print += lineItem.getDescription() + "\t";
            print += lineItem.getPrice() + "\t";
            print += lineItem.getQuantity() + "\t";
            print += lineItem.totalAmount() + "\n";
        }
        return print;
    }

    private String appendCostumerInfo() {
        String print = "======Printing Orders======\n";
        print += order.getCustomerName();
        print += order.getCustomerAddress();
        return print;
    }
}